package com.example.group.network.express_plus_app.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.group.network.express_plus_app.MyReceiveOrdersActivity;
import com.example.group.network.express_plus_app.MyReceiveOrdersShowDetailActivity;
import com.example.group.network.express_plus_app.R;
import com.example.group.network.express_plus_app.WaitFinishShowDetailActivity;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.widget.RecyclerExtras;

import java.util.ArrayList;

/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/2/24 13:20
 */
public class MyReceiveOrdersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements RecyclerExtras.OnItemClickListener {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<Order> mOrders;
    public MyReceiveOrdersAdapter(Context mContext, ArrayList<Order> mPublicArray) {
        this.mContext = mContext;
        this.mOrders = mPublicArray;
        this.mInflater=LayoutInflater.from(mContext);
    }

    //创建列表项的视图持有者
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=null;
        RecyclerView.ViewHolder holder=null;
        view=mInflater.inflate(R.layout.my_receive_orders_list_linear,parent,false);
        holder = new ItemHolder(view);
        return holder;
    }

    //绑定列表项的持有者
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        ItemHolder holder= (ItemHolder) viewHolder;
        holder.tv_order_type.setText(mOrders.get(position).getExpressType());
        holder.tv_user_phone.setText(mOrders.get(position).getUserPhone());
        holder.tv_express_detail_address.setText(mOrders.get(position).getExpressDetailAddress());
        holder.tv_delivery_address.setText(mOrders.get(position).getDeliveryAddress());
        holder.tv_delivery_mode.setText(mOrders.get(position).getDeliveryMode());
        holder.tv_user_leisure.setText(mOrders.get(position).getUserLeisure());
        //列表项的点击事件需要自己实现
        //点击事件
        holder.cv_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mOrders.size();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    //在列表项被点击时调用
    @Override
    public void onItemClick(View view, int position) {
        Bundle bundle=new Bundle();
        Order order = mOrders.get(position);
        bundle.putSerializable("ORDER",order);
        Intent intent = new Intent(mContext, MyReceiveOrdersShowDetailActivity.class);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );//设置标记，当在activity使用startActivity时使用
        mContext.startActivity(intent);
    }
    public class ItemHolder extends RecyclerView.ViewHolder{

        public CardView cv_item;
        public TextView tv_order_type;
        public TextView tv_user_phone;
        public TextView tv_express_detail_address;
        public TextView tv_delivery_address;
        public TextView tv_delivery_mode;
        public TextView tv_user_leisure;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            cv_item=itemView.findViewById(R.id.cv_item);
            tv_order_type=itemView.findViewById(R.id.tv_order_type);
            tv_user_phone=itemView.findViewById(R.id.tv_user_phone);
            tv_express_detail_address=itemView.findViewById(R.id.tv_express_detail_address);
            tv_delivery_address=itemView.findViewById(R.id.tv_delivery_address);
            tv_delivery_mode=itemView.findViewById(R.id.tv_delivery_mode);
            tv_user_leisure=itemView.findViewById(R.id.tv_user_leisure);
        }
    }
    private RecyclerExtras.OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(RecyclerExtras.OnItemClickListener listener){
        this.mOnItemClickListener=listener;
    }
}
